package lyl.xml.jdom;

import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.Text;
import org.jdom.output.EscapeStrategy;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * @author Liu Yulong
 */
public class JdomTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Document doc = new Document();
        Element root = new Element("workflow");
        root.setAttribute("creator", "");
        root.setAttribute("version", "");
        root.setAttribute("desc", "");
        root.setAttribute("createdatetime", "");
        doc.setRootElement(root);

        Element e1 = new Element("constants");
        Element e11 = new Element("constant");
        e11.setAttribute("name", "BusinessRSType");
        e11.setAttribute("value", "ucms-info");
        e1.addContent(e11);
        Element e12 = new Element("constant");
        e12.setAttribute("name", "constant2");
        e12.setAttribute("value", "1");
        e1.addContent(e12);

        Element e2 = new Element("varialbles");
        Element e21 = new Element("varialble");
        e21.setAttribute("name", "BusinessRSID");
        e21.setAttribute("initvalue", "");

        Element e3 = new Element("steps");
        Element e31 = new Element("step");
        e31.setAttribute("id", "1");
        e31.setAttribute("stepname", "录入(开始)");
        e31.setAttribute("steptype", "start");
        e31.setAttribute("stepdesc", "");
        e31.setAttribute("apptype", "start");
        Element e311 = new Element("actions");
        Element e3111 = new Element("action");
        e3111.setAttribute("id", "0");
        e3111.setAttribute("code", "create");
        Element e31111 = new Element("links");
        Element e31112 = new Element("pre-functions");
        Element e311121 = new Element("function");
        e311121.setAttribute("type", "javaapi");
        e311121.setAttribute("classname", "");
        e311121.setAttribute("methodnanme", "");
        Element e31113 = new Element("post-functions");
        Element e311131 = new Element("function");
        e311131.setAttribute("type", "javaapi");
        e311131.setAttribute("classname", "");
        e311131.setAttribute("methodnanme", "");
        Element e3111311 = new Element("args");
        Element e31113111 = new Element("arg");
        e31113111.setAttribute("name", "username");
        e31113111.setAttribute("type", "String");
        e31113111.setAttribute("sortno", "1");
        Element e31113112 = new Element("arg");
        e31113112.setAttribute("name", "password");
        e31113112.setAttribute("type", "long");
        e31113112.setAttribute("sortno", "2");
        Element e311111 = new Element("link");
        e311111.setAttribute("id", "1");
        e311111.setAttribute("nextstep", "2");
        Element e3111111 = new Element("conditions");
        Element e31111111 = new Element("condition");
        e31111111.setAttribute("id", "");
        e31111111.setAttribute("seq", "1");
        e31111111.setAttribute("expression", "#var1#>50000");
        e31111111.setAttribute("nextop", "and");
        e3111111.addContent(e31111111);
        e3111311.addContent(e31113111);
        e3111311.addContent(e31113112);
        e311111.addContent(e3111111);
        e311131.addContent(e3111311);
        e31111.addContent(e311111);
        e31112.addContent(e311121);
        e31113.addContent(e311131);
        e3111.addContent(e31111);
        e3111.addContent(e31112);
        e3111.addContent(e31113);
        e311.addContent(e3111);
        e31.addContent(e311);
        e3.addContent(e31);
        e2.addContent(e21);
        root.addContent(e1);
        root.addContent(e2);
        root.addContent(e3);
        Format format = Format.getPrettyFormat();
        format.setEncoding("UTF-8");
        format.setEscapeStrategy(new EscapeStrategy() {
            public boolean shouldEscape(char ch) {
                return false;
            }
        });
        org.jdom.output.XMLOutputter outputter = new XMLOutputter(format);
        try {
            outputter.output(doc, new FileWriter("d:\\test.xml"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test() {
        Document doc = new Document();
        Namespace ns = Namespace.getNamespace("http://www.ucap.com.cn");
        Namespace ns2 = Namespace.getNamespace("other", "http://www.w3c.org");
        Element root = new Element("workflow", ns);
        root.addNamespaceDeclaration(ns2);
        doc.setRootElement(root);
        Element el1 = new Element("元素一");
        el1.setAttribute("属性", "属性一");
        Text text1 = new Text("元素值");
        Element em = new Element("元素二").addContent("第二个元素");
        el1.addContent(text1);
        el1.addContent(em);
        Element el2 = new Element("元素三").addContent("第三个元素");
        root.addContent(el1);
        root.addContent(el2);
        // 缩进四个空格,自动换行,gb2312编码
        Format format = Format.getPrettyFormat();
        format.setEncoding("UTF-8");
        org.jdom.output.XMLOutputter outputter = new XMLOutputter(format);
        try {
            outputter.output(doc, new FileWriter("d:\\test.xml"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}